package broker

import (
	"context"
	"github.com/asim/go-micro/plugins/broker/rabbitmq/v4"
	"go-micro.dev/v4/broker"
	"signal/config"
	"signal/utils"
)

var defaultRabbitMQAddress = "amqp://guest:guest@localhost:5672"

func InitRabbit() broker.Broker {
	if config.Conf.Broker.Addr != "" {
		defaultRabbitMQAddress = config.Conf.Broker.Addr
	}
	b := rabbitmq.NewBroker(broker.Addrs(defaultRabbitMQAddress))
	utils.Throw(b.Connect())
	return b
}

func InitAliRabbit() broker.Broker {
	if config.Conf.Broker.Addr != "" {
		defaultRabbitMQAddress = config.Conf.Broker.Addr
	}

	b := NewBroker(broker.Addrs(defaultRabbitMQAddress))
	utils.Throw(b.Connect())

	return b
}

func NewBroker(opts ...broker.Option) broker.Broker {
	options := broker.Options{
		Context: context.Background(),
	}

	for _, o := range opts {
		o(&options)
	}

	broker := &AmqpRabbitMq{
		addrs: options.Addrs,
		opts:  options,
	}
	broker.Init(opts...)
	return broker
}

